package com.easygo.common.vo;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data
@Slf4j
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    private static Integer SUCCESSCODE = 200;

    private static Integer ERRORCODE = 500;

    /**
     * 返回处理消息
     */
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = code;
        this.success = true;
        return this;
    }


    public static Result<Object> ok() {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(SUCCESSCODE);
        r.setMessage("成功");
        return r;
    }

    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(SUCCESSCODE);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(SUCCESSCODE);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> successData(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(SUCCESSCODE);
        r.setResult(data);
        //log.info("r------------------{}",r);
        return r;
    }

    public static <T> Result<T> error(String msg) {
        return error(ERRORCODE, msg);
    }

    public static Result<Object> error() {
        Result<Object> r = new Result<Object>();
        r.setCode(ERRORCODE);
        r.setMessage("请求错误");
        r.setSuccess(false);
        return r;
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = ERRORCODE;
        this.success = false;
        return this;
    }
    /**
     * 无权限访问返回结果
     */
//	public static Result<Object> noauth(String msg) {
//		return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
//	}
}
